<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;//声明命名空间

use \think\Cache;//调用这个类的方法
use \think\Controller;
use think\Loader;
use think\Db;
use \think\Cookie;
use \think\Session;
use app\admin\model\ Author as articleModel;
use app\admin\model\Nav as cateModel;
class  Author extends Permissions
{
    public function index()
    {
        $model = new articleModel();
        $post = $this->request->param();//获取页面以post传值的参数
        if (isset($post['keywords']) and !empty($post['keywords'])) {
            $where['title'] = ['like', '%' . $post['keywords'] . '%'];
        }
        if (isset($post['nav_id']) and $post['nav_id'] > 0) {
            $where['nav_id'] = $post['nav_id'];
        }

        if (isset($post['status']) and ($post['status'] == 1 or $post['status'] === '0')) {
            $where['status'] = $post['status'];
        }
        if(isset($post['create_time']) and !empty($post['create_time'])) {
            $min_time = strtotime($post['create_time']);
            $max_time = $min_time + 24 * 60 * 60;
            $where['create_time'] = [['>=',$min_time],['<=',$max_time]];
        }
        //三目运算符，判断是否有查询条件，如果没有就查询所有数据，如果有就按照条件查询
        $articles = empty($where) ? $model->order('create_time desc')->paginate(20) : $model->where($where)->order('create_time desc')->paginate(20,false,['query'=>$this->request->param()]);
//this−>assign()将后台数据传递到前台页面中tp5.1以前this->assign()用法
        $this->assign('author',$articles);
        $info['cate'] = Db::name('nav')->select();
        $info['admin'] = Db::name('admin')->select();
        $this->assign('info',$info);
        return $this->fetch();//映射V层页面
    }


    public function publish()
    {
        //获取菜单id
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        $model = new articleModel();
        $cateModel = new cateModel();
        //是正常添加操作
        if($id > 0) {
            //是修改操作
            if($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                $validate = new \think\Validate([
                    ['title', 'require', '标题不能为空'],
                    ['nav_id', 'require', '请选择分类'],
                    ['thumb', 'require', '请上传缩略图'],
                ]);
                //验证部分数据合法性
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }
                //验证菜单是否存在
                $article = $model->where('id',$id)->find();
                if(empty($article)) {
                    return $this->error('id不正确');
                }

                if(false == $model->allowField(true)->save($post,['id'=>$id])) {
                    return $this->error('修改失败');
                } else {
                    addlog($model->id);//写入日志
                    return $this->success('修改成功','admin/author/index');
                }
            } else {
                //非提交操作
                $article = $model->where('id',$id)->find();
                $cates = $cateModel->select();
                $cates_all = $cateModel->menulist($cates);
                $this->assign('cates',$cates_all);
                if(!empty($article)) {
                    $this->assign('article',$article);
                    return $this->fetch();
                } else {
                    return $this->error('id不正确');
                }
            }
        } else {
            //是新增操作
            if($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                $validate = new \think\Validate([
                    ['title', 'require', '标题不能为空'],
                    ['nav_id', 'require', '请选择分类'],
                    ['thumb', 'require', '请上传缩略图'],
                ]);
                //验证部分数据合法性
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }

                if(false == $model->allowField(true)->save($post)) {
                    return $this->error('添加失败');
                } else {

                    return $this->success('添加成功','admin/author/index');
                }
            } else {
                //非提交操作
                $cate = $cateModel->select();
                $cates = $cateModel->menulist($cate);
                $this->assign('cates',$cates);
                return $this->fetch();
            }
        }

    }


    public function delete()
    {
        if($this->request->isAjax()) {
            $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            if(false == Db::name('author')->where('id',$id)->delete()) {
                return $this->error('删除失败');
            } else {
                addlog($id);//写入日志
                return $this->success('删除成功','admin/author/index');
            }
        }
    }
    public function audit()
    {
        //获取文件id
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if($id > 0) {
            if($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                $status = $post['status'];
                if(false == Db::name('author')->where('id',$id)->update(['status'=>$status])) {
                    return $this->error('审核提交失败');
                } else {
                    addlog($id);//写入日志
                    return $this->success('审核提交成功','admin/author/index');
                }
            }
        } else {
            return $this->error('id不正确');
        }
    }
    public function orders()
    {
        if($this->request->isPost()) {
            $post = $this->request->post();
            $i = 0;
            foreach ($post['id'] as $k => $val) {
                $order = Db::name('author')->where('id',$val)->value('orders');
                if($order != $post['orders'][$k]) {
                    if(false == Db::name('author')->where('id',$val)->update(['orders'=>$post['orders'][$k]])) {
                        return $this->error('更新失败');
                    } else {
                        $i++;
                    }
                }
            }
            addlog();//写入日志
            return $this->success('成功更新'.$i.'个数据','admin/author/index');
        }
    }


}
